package sui.utils.xml;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

/**
 * @Description TODO
 * @Author XQF.Sui
 * @Date 2020/7/24 9:31 上午
 * @Version 1.0
 */
public class AnalyzeXML {

  private static int num = 1;
  private static int level = 1;

  public static void readXml(File file) {
    SAXReader reader = new SAXReader();
    Map<String, Object> resultMap = new HashMap<>();

    try {
      Document document = reader.read(file);
//      de(document.getRootElement(), resultMap);

      de(document.getRootElement(), null, level);

      System.out.println(JSON.toJSONString(resultMap));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

//  public static void de(Element ele, Map<String, Object> resultMap) {
//    Iterator<Element> iterator = ele.elementIterator();
//    List<Map<String, Object>> eleList = new ArrayList<>();
//
//    while (iterator.hasNext()) {
//      Element element = iterator.next();
//      Iterator<Attribute> attributeIterator = element.attributeIterator();
//      Map<String, Object> dataMap = new HashMap<>();
//
//      System.out.println("==================");
//      for (Iterator<Attribute> it = attributeIterator; it.hasNext(); ) {
//        Attribute attribute = it.next();
//
//        System.out.println(attribute.getName() + " - " + attribute.getValue());
//
//        dataMap.put(attribute.getName(), attribute.getValue());
//      }
//
//      eleList.add(dataMap);
//      de(element, dataMap);
//    }
//
//    System.out.println("==================");
//    resultMap.put("sub", eleList);
//  }

  public static void de(Element ele, Integer parentCode, int level) {
    Iterator<Element> iterator = ele.elementIterator();
    List<Map<String, Object>> eleList = new ArrayList<>();
    int sort = 1;

    while (iterator.hasNext()) {
      Element element = iterator.next();
      Iterator<Attribute> attributeIterator = element.attributeIterator();
      int code = num++;

      StringBuffer field = new StringBuffer("insert into t_dic (code, type, level, sort, system_id");
      StringBuffer value = new StringBuffer(" ) values (")
              .append(code).append(", ")
              .append("'INTERNATIONAL_LOCATION'").append(", ")
              .append(level).append(", ")
              .append(sort++).append(", ")
              .append(6);

      boolean isPrint = false;

      for (Iterator<Attribute> it = attributeIterator; it.hasNext(); ) {
        isPrint = true;
        Attribute attribute = it.next();
//        System.out.println(attribute.getName() + " - " + attribute.getValue());

        if (StringUtils.equals(attribute.getName(), "Code")) {
          field.append(", ").append("vcode");
        } else {
          field.append(", ").append(attribute.getName());
        }


        value.append(',').append('\'').append(attribute.getValue()).append('\'');
      }

      field.append(value).append(");");

      if (parentCode != null && isPrint) {
        StringBuffer parent = new StringBuffer("insert into t_dic_ref (code, type, parent_code, parent_type) values (");
        parent.append(code).append(", ")
                .append("'INTERNATIONAL_LOCATION'").append(", ")
                .append(parentCode).append(", ")
                .append("'INTERNATIONAL_LOCATION'").append(");");

//        System.out.println(parent.toString());
      }

      if (isPrint) {
        System.out.println(field.toString());
      }

      de(element, code, level + 1);
    }

  }

  public static void main(String[] args) {
    readXml(new File("/Users/suigaopeng/Library/Containers/com.tencent.xinWeChat/Data/Library/Application Support/com.tencent.xinWeChat/2.0b4.0.9/8058aeaa2920fa3da20b087d7d24fd25/Message/MessageTemp/f8f9ee0b7fb8bd341071e562eb6c599a/File/LocList.xml"));
  }
}
